import os  

# 数据存储文件名  
TODO_FILE = "todo_list.txt"  

def load_tasks():  
    """加载任务列表"""  
    if not os.path.exists(TODO_FILE):  
        return []  
    with open(TODO_FILE, "r") as f:  
        return f.read().splitlines()  

def save_tasks(tasks):  
    """保存任务列表"""  
    with open(TODO_FILE, "w") as f:  
        f.write("\n".join(tasks))  

def add_task(task):  
    """添加任务"""  
    tasks = load_tasks()  
    tasks.append(task)  
    save_tasks(tasks)  
    print("任务已添加！")  

def list_tasks():  
    """列出所有任务"""  
    tasks = load_tasks()  
    if not tasks:  
        print("当前没有任务。")  
    else:  
        print("\n待办事项列表：")  
        for idx, task in enumerate(tasks, 1):  
            print(f"{idx}. {task}")  

def delete_task(task_id):  
    """删除任务"""  
    tasks = load_tasks()  
    if 1 <= task_id <= len(tasks):  
        removed_task = tasks.pop(task_id - 1)  
        save_tasks(tasks)  
        print(f"已删除任务：{removed_task}")  
    else:  
        print("无效的任务编号！")  

if __name__ == "__main__":  
    while True:  
        print("\n===== 待办事项管理器 =====")  
        print("1. 添加任务")  
        print("2. 查看任务")  
        print("3. 删除任务")  
        print("4. 退出")  

        choice = input("请输入选项 (1-4): ")  

        if choice == "1":  
            task = input("请输入任务内容：")  
            add_task(task)  
        elif choice == "2":  
            list_tasks()  
        elif choice == "3":  
            list_tasks()  
            if load_tasks():  
                task_id = int(input("请输入要删除的任务编号："))  
                delete_task(task_id)  
        elif choice == "4":  
            print("再见！")  
            break  
        else:  
            print("无效选项，请重新输入！")  